/*
 * @Author: houmin 745762351@qq.com
 * @Date: 2023-11-29 10:06:07
 * @LastEditors: houmin 745762351@qq.com
 * @LastEditTime: 2023-12-01 10:56:08
 * @FilePath: \tch-h5\src\utils\check.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
export const isValidChineseID =(idNumber) => {
    // 校验身份证号码长度和格式
    if (!/^\d{17}[\dX]$/.test(idNumber)) {
        return false
    }

    // 校验校验位
    const idArray = idNumber.split('').map(Number)
    const factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
    const checkCodes = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
    let sum = 0
    for (let i = 0; i < 17; i++) {
        sum += idArray[i] * factor[i]
    }
    const remainder = sum % 11
    if (checkCodes[remainder] !== idNumber[17].toUpperCase()) {
        return false
    }

    // 判断出生日期合法性
    const year = parseInt(idNumber.slice(6, 10))
    const month = parseInt(idNumber.slice(10, 12))
    const day = parseInt(idNumber.slice(12, 14))

    if (year < 1900 || year > new Date().getFullYear()) {
        return false
    }

    if (month < 1 || month > 12) {
        return false
    }

    if (day < 1 || day > 31) {
        return false
    }

    const maxDay = new Date(year, month, 0).getDate();
    if (day > maxDay) {
        return false
    }
    return true
}

export const isValidPhoneNumber = (phoneNumber) => {
    const phonePattern = /^1[3-9]\d{9}$/
    return  phonePattern.test(phoneNumber)
}

